package cn.ava.product.service.impl;

import cn.ava.basic.constant.BaseConstants;
import cn.ava.basic.utils.BaiDuAiCensorUtil;
import cn.ava.product.domain.Product;
import cn.ava.product.domain.ProductDetail;
import cn.ava.product.domain.ProductOnlineAuditLog;
import cn.ava.product.domain.Product;
import cn.ava.product.domain.ProductDetail;
import cn.ava.product.domain.ProductOnlineAuditLog;
import cn.ava.product.mapper.ProductDetailMapper;
import cn.ava.product.mapper.ProductMapper;
import cn.ava.product.mapper.ProductOnlineAuditLogMapper;
import cn.ava.product.service.IProductService;
import cn.ava.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ava
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;
    
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /**
     * @Title: getByIdDetail
     * @Description: 查询商品详情
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/21 10:16
     * @param:
     * @return:
     */
    @Override
    public ProductDetail getByIdDetail(Long id) {
        return productMapper.getByIdDetail(id);
    }

    @Override
    public void delete(Long id) {
        Product product = productMapper.getById(id);
        productMapper.delete(id);
        productDetailMapper.deleteByProductId(product.getId());
    }
    
    
    @Override
    public void add(Product product) {
        productMapper.add(product);

        ProductDetail productDetail = product.getDetail();

        if (!StringUtils.isEmpty(productDetail)){
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
        ProductDetail productDetail = product.getDetail();
        productDetailMapper.deleteByProductId(product.getId());
        if (!StringUtils.isEmpty(productDetail)){
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }

    /**
     * @Title: offsale
     * @Description: 批量下架
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/16 22:21
     * @param:
     * @return:
     */
    @Override
    public void offsale(List<Long> ids) {
        productMapper.offsale(ids);
    }
    /**
     * @Title: onsale
     * @Description: 批量上架
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/16 22:21
     * @param:
     * @return:
     */
    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.getById(id);
            StringBuilder sb = new StringBuilder();
            if (!BaiDuAiCensorUtil.TextCensor(product.getName())){
                sb.append("商品名字违规,违规信息: ").append(product.getName());
            }
            for (String Images : product.getResources().split(",")) {
                if (!BaiDuAiCensorUtil.ImgCensor(BaseConstants.VerifyShopSettlement.FASTDFS_IMAGE + Images)) {
                    sb.append("商品图片违规,违规信息: ").append(BaseConstants.VerifyShopSettlement.FASTDFS_IMAGE).append(Images);
                }
            }
            if (sb.length() > 0) {
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(product.getId());
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setNote(sb.toString());
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLogMapper.add(productOnlineAuditLog);
                continue;
            }
            productMapper.onsale(id);
        }
    }






}
